package weaver.interfaces.hitech.duss.action.zdqsq;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-07-28
 * @Description: 招待券生成号码
 * @ModifyLog:
 **/

public class WeaZdqsqAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        BaseBean baseBean = new BaseBean();
        try {
            String requestid = requestInfo.getRequestid();
            baseBean.writeLog("WeaZdqsqAction--requestID:" + requestid);
            String tablename = requestInfo.getRequestManager().getBillTableName();//主表
            rs.executeQuery("select zjsl,sqsjvalue,sqsj,zdqhm,xyzqdcqhm from " + tablename + " where requestid='" + requestid + "'");
            rs.next();
            String zjsl = Util.null2String(rs.getString("zjsl"));//总计数量
            String sqsjvalue = Util.null2String(rs.getString("sqsjvalue"));//期限
            String sqsj = Util.null2String(rs.getString("sqsj"));//申请时间
            String zdqhm = Util.null2String(rs.getString("zdqhm"));//招待券号码
            String xyzqdcqhm = Util.null2String(rs.getString("xyzqdcqhm"));//下周期号码
            String str[] = sqsj.split("-");
            String curyear = str[0];
            String curmonth = str[1];
            String curday = str[2].substring(0, 2);
            String sqsj1 = sqsjvalue.substring(sqsjvalue.indexOf("(") + 1, sqsjvalue.indexOf(")"));
            String str1[] = sqsj1.split("-");
            String year1 = str1[0];
            String month1 = str1[1];
            String day1 = str1[2];
            String year2 = str1[3];
            String month2 = str1[4];
            String day2 = str1[5];
            String date1 = year1 + "-" + month1 + "-" + day1;
            String date2 = year2 + "-" + month2 + "-" + day2;
            String curdate = curyear + "-" + curmonth + "-" + curday;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date Date1 = sdf.parse(date1);
            Date Date2 = sdf.parse(date2);
            Date CurDate = sdf.parse(curdate);
            int i = Date1.compareTo(CurDate);
            int j = Date2.compareTo(CurDate);
            if ((i == -1 || i == 0) && (j == 1)) {
                int counts = Util.getIntValue(zjsl) + Util.getIntValue(zdqhm);
                int zdqhm1 = Util.getIntValue(zdqhm) + 1;
                String cqkshm = String.format("%0" + 3 + "d", zdqhm1);
                String cqjshm = String.format("%0" + 3 + "d", counts);
                rs1.executeUpdate("update " + tablename + " set cqkshm='" + cqkshm + "',cqjshm='" + cqjshm + "' where requestid='" + requestid + "'");
                baseBean.writeLog("update " + tablename + " set cqkshm='" + cqkshm + "',cqjshm='" + cqjshm + "' where requestid='" + requestid + "'");
            } else {
                int counts = Util.getIntValue(zjsl) + Util.getIntValue(xyzqdcqhm);
                int xyzqdcqhm1 = Util.getIntValue( xyzqdcqhm ) + 1;
                String cqkshm = String.format( "%0" + 3 + "d", xyzqdcqhm1 );
                String cqjshm = String.format( "%0" + 3 + "d", counts );
                rs1.executeUpdate( "update " + tablename + " set cqkshm='" + cqkshm + "',cqjshm='" + cqjshm + "' where requestid='" + requestid + "'" );
                baseBean.writeLog( "update " + tablename + " set cqkshm='" + cqkshm + "',cqjshm='" + cqjshm + "' where requestid='" + requestid + "'" );
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseBean.writeLog("异常参考:" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }

        return Action.SUCCESS;
    }
}
